package com.qf.lanzhou.blog.api.controller.login;

import com.qf.lanzhou.blog.api.controller.BaseServletPost;
import com.qf.lanzhou.blog.api.pojo.entity.Audit;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.AdminAuditService;
import com.qf.lanzhou.blog.api.service.PersonAuditService;
import com.qf.lanzhou.blog.api.service.impl.login.AdminAuditServiceImpl;
import com.qf.lanzhou.blog.api.service.impl.login.PersonAuditServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LEON-HE
 */
@WebServlet("/showMsgToPerson.do")
public class ShowMsgToPersonServlet extends BaseServletPost {
    private PersonAuditService auditService = new PersonAuditServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Audit> audits = new ArrayList<>();
        User user = (User)req.getSession().getAttribute("user");
        try{
            audits = auditService.getPersonList(user.getId());
            for (Audit audit:audits) {
                audit.setSendTimeStr(audit.getSendTime());
            }
            successResultWrite(audits,resp);
        }catch (RuntimeException e){
            e.printStackTrace();
            String errormsg = e.getMessage();
            failedResultWrite(errormsg,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
